<?php session_start();
#######################################################
# WebSam: Web Server Ascent Manager    	              #
# Website: http://websam.googlecode.com               #
#-----------------------------------------------------#
# Install File: This will install your websam Portal  #
#-----------------------------------------------------#
# WebSam is a GPL V3 program, and can be used by      #
# anyone and can be edittedby anyone! 	              #
#-----------------------------------------------------#
# But we can ask for mods!         		              #
#                                  	                  #
#                                  	                  #
#######################################################
/* this is the main page of the installer script..    */
/* this script will edit the config files automaticly */
/* depending on what the user puts                    */
/* ************************************************** */

	?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>WebSam Installer</title>
		</head>
		
		<style>
		body
		{
			background:#E5E5E5;
		}
		
		.body
		{
			background:#FFFFFF;
			border:#000000 groove 1px;
			margin:auto;
			width:690px;
			min-height:300px;
			padding:5px;
			text-align:left;
			font-family:Verdana, Arial, Helvetica, sans-serif;
			font-size:12px;
		}
		
		.title
		{
			background:#FFFFFF;
			border:#000000 groove 1px;
			border-bottom:none;
			margin:auto;
			margin-top:50px;
			width:690px;
			height:20px;
			padding:5px;
			text-align:left;
			vertical-align:middle;
			font-family:Verdana, Arial, Helvetica, sans-serif;
			font-style:italic;
			font-weight:bolder;
			font-size:16px;
		}
		
		input
		{
			background:#FFFFFF;
			border:#000000 solid 1px;
			padding:1px;
		}
		
		table
		{
			margin:auto;
		}
		
		td
		{
			text-align:center;
			vertical-align:middle;
			width:250px;
			border-bottom:#000000 solid 1px;
		}
		</style>
		<body>
	<?php
	clearstatcache();
	//error_reporting(E_ERROR^E_ALL);
	require_once("../includes/sql.php");

	if(file_exists("install.lock"))
	{
		$_SESSION['install']['error']="<li>The installer is locked. You are seeing this message because WebSAM is already installed.</li>";
		$_POST['step']="error";
	}

	switch($_POST['step'])
	{
	case "4":
		$dbhost = $_POST['dbhost'];
		$dbuser = $_POST['dbuser'];
		$dbpass = $_POST['dbpass'];
		$dbname = $_POST['dbname'];
		$dbport = $_POST['dbport'];
		$tablepref = $_POST['tablepref'];
		$logondb = $_POST['logondb'];
		$worlddb = $_POST['worlddb'];
		$chardb = $_POST['worlddb'];
		$servername = $_POST['servername'];
		$ascentpidpath = $_POST['ascentpidpath'];
		$logonpidpath = $_POST['logonpidpath'];
		$webmaster_name = $_POST['webmaster_name'];
		$webmaster_email = $_POST['webmaster_email'];
		$sqltype = $_POST['software'];
		
		//Test connection.
		$sql = new SQL($dbhost,$dbuser,$dbpass,$dbname,$sqltype);
		if($sql->Connection == NULL) { $_SESSION['install']['error'].="<li>Could not connect to SQL database. Additional info: ".$sql->Error.".</li>"; }
		
		
		//Begin writing tables.
		
		
		if($_SESSION['install']['error']!=NULL) { ?>
														<div class="title"><img src="../images/websam.gif" height="20" width="20" /> Error</div>	
														<div class="body">The installation could not progress due to the following reasons:<br />
														<br />
														<ul />
														<?php echo $_SESSION['install']['error']; ?>
														</ul>
														</div>
													<?php
													unset($_SESSION['install']); die; }
		
		$content = "<?php
			#######################################################
			# WebSam: Web Server Ascent Manager.                  #
			# Website: http://websam.googlecode.com               #
			#-----------------------------------------------------#
			# Config file: this file holds all of your sites info #
			#-----------------------------------------------------#
			# WebSam is a GPL V3 program, and can be used by      #
			# anyone and can be edittedby anyone! 	            #
			#-----------------------------------------------------#
			# But we can ask for mods!    	     	            #
			#                                  	                  #
			#                                  	                  #
			#######################################################
			/* this is the main page of the installer script..    */
			/* this script will edit the config files automaticly */
			/* depending on what the user puts                    */
			/* ************************************************** */
			
			/* variables you can edit these by hand but the install tool is easyer */
			
			define(DBHOST,'{$dbhost}');
			define(DBUSER,'{$dbuser}');
			define(DBPASS,'$dbpass');
			define(DBNAME,'$dbname');
			define(DBPORT,'$dbport');
			define(TABLEPREF,'$tablepref');
			define(LOGONDB,'$logondb');
			define(WORLDDB,'$worlddb');
			define(CHARDB,'$chardb');
			define(SERVERNAME,'$servername');
			define(ASCENTPIDPATH,'$ascentpidpath');
			define(LOGONPIDPATH,'$logonpidpath');
			define(WEBMASTER_NAME,'$webmaster_name');
			define(WEBMASTER_EMAIL,'$webmaster_email');
			define(SQLTYPE,'$sqltype');
			?>";
			unset ($dbpass);
		$f = fopen("../includes/config.php","w+");
		fwrite($f,$content);
		fclose($f);
		$f = fopen("install.lock","w+");
		fwrite($f,"Installer is locked.");
		fclose($f);
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> WebSAM Successfully Installed!</div>	
			<div class="body">Congratulations! WebSAM has been successfully installed on your server!<br />
			<br />
			<br />
			<br />
			</div>
		<?php
		break;
	case "3":
		$dbhost = $_POST['dbhost'];
		$dbuser = $_POST['dbuser'];
		$dbpass = $_POST['dbpass'];
		$dbname = $_POST['dbname'];
		$dbport = $_POST['dbport'];
		$tablepref = $_POST['tablepref'];
		$logondb = $_POST['logondb'];
		$worlddb = $_POST['worlddb'];
		$chardb = $_POST['worlddb'];
		$servername = $_POST['servername'];
		$ascentpidpath = $_POST['ascentpidpath'];
		$logonpidpath = $_POST['logonpidpath'];
		$webmaster_name = $_POST['webmaster_name'];
		$webmaster_email = $_POST['webmaster_email'];
		$sqltype = $_POST['software'];
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> Review Information</div>	
			<div class="body">Please review the information you have entered to make sure it is correct.<br />
			<br />
			<br />
			<br />
			<form method="post">
			<input type="hidden" name="step" value="4" />
				<table width="500" border="0" cellspacing="5px">
					<tr>
					<td>Database Software:</td><td><input type="hidden" name="software" value="<?php echo $sqltype; ?>" /><?php echo $sqltype; ?></td>
					</tr>
					<tr>
					<td>Database Host:</td><td><input type="hidden" name="dbhost" value="<?php echo $dbhost; ?>" /><?php echo $dbhost; ?></td>
					</tr>
					<tr>
					<td>Username:</td><td><input type="hidden" name="dbuser" value="<?php echo $dbuser; ?>" /><?php echo $dbuser; ?></td>
					</tr>
					<tr>
					<td>Password:</td><td><input type="hidden" name="dbpass" value="<?php echo $dbpass; ?>" /><?php echo $dbpass; ?></td>
					</tr>
					<?php
					if($_SESSION['install']['software']=="mysql"){
					?>
					<tr>
					<td>Port</td><td><input type="hidden" name="dbport" value="<?php echo $dbport; ?>" /><?php echo $dbport; ?></td>
					</tr>
					<?
					}else{
					};
					?>
					<tr>
					<td>Logon Database:</td><td><input type="hidden" name="logondb" value="<?php echo $logondb; ?>" /><?php echo $logondb; ?></td>
					</tr>
					<tr>
					<td>World Database:</td><td><input type="hidden" name="worlddb" value="<?php echo $worlddb; ?>" /><?php echo $worlddb; ?></td>
					</tr>
					<tr>
					<td>Character Database:</td><td><input type="hidden" name="chardb" value="<?php echo $chardb; ?>" /><?php echo $chardb; ?></td>
					</tr>
					<tr>
					<td>WebSam Database:</td><td><input type="hidden" name="dbname" value="<?php echo $dbname; ?>" /><?php echo $dbname; ?></td>
					</tr>
					<tr>
					<td>Table Prefix:</td><td><input type="hidden" name="tablepref" value="<?php echo $tablepref; ?>"/><?php echo $tablepref; ?></td>
					<tr>
					<tr>
					<td>Server Name:</td><td><input type="hidden" name="servername" value="<?php echo $servername; ?>" /><?php echo $servername; ?></td>
					</tr>
					<tr>
					<td>Path to ascent.pid</td><td><input type="hidden" name="ascentpidpath" value="<?php echo $ascentpidpath; ?>" /><?php echo $ascentpidpath; ?></td>
					</tr>
					<tr>
					<td>Path to logon.pid</td><td><input type="hidden" name="logonpidpath" value="<?php echo $logonpidpath; ?>" /><?php echo $logonpidpath; ?></td>
					</tr>
					<tr>
					<td>Webmaster's Name:</td><td><input type="hidden" name="webmaster_name" value="<?php echo $webmaster_name; ?>" /><?php echo $webmaster_name; ?></td>
					</tr>
					<tr>
					<td>Webmaster's Email:</td><td><input type="hidden" name="webmaster_email" value="<?php echo $webmaster_email; ?>" /><?php echo $webmaster_email; ?></td>
					</tr>
					<tr>
					<td><input type="button" value="Go Back" onclick="history.go(-1);" /></td><td><input type="submit" value="Proceed" />
					</tr>
				</table>
			</form>
			</div>
		<?php
		break;
	case "2":
		$sqltype = $_POST['software'];
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> Database Information</div>	
			<div class="body">Please fill out the following information about your database. 
			Please make sure you enter the information correctly, as this is a very important part of installation.<br />
			<br />
			<br />
			<br />
			<form method="post">
			<input type="hidden" name="step" value="3" />
			<input type="hidden" name="software" value="<?php echo $sqltype; ?>" />
				<table width="500" border="0" cellspacing="5px">
					<tr>
					<td>Database Host:</td><td><input type="text" name="dbhost" value="localhost" /></td>
					</tr>
					<tr>
					<td>Username:</td><td><input type="test" name="dbuser" value="root" /></td>
					</tr>
					<td>Password:</td><td><input type="text" name="dbpass" /></td>
					</tr>
					<?php
					if($sqltype=="postgresql"){
					?>
					<tr>
					<td>Port</td><td><input type="text" name="dbport" value="3306" /></td>
					</tr>
					<?
					}else{
					};
					?>
					<tr>
					<td>Logon Database:</td><td><input type="text" name="logondb" /></td>
					</tr>
					<tr>
					<td>World Database:</td><td><input type="text" name="worlddb" /></td>
					</tr>
					<tr>
					<td>Character Database:</td><td><input type="text" name="chardb" /></td>
					</tr>
					<tr>
					<td>WebSam Database:</td><td><input type="text" name="dbname" /></td>
					</tr>
					<tr>
					<td>Table Prefix:</td><td><input type="text" name="tablepref" value="websam_" /></td>
					<tr>
					<tr>
					<td>Server Name:</td><td><input type="text" name="servername" /></td>
					</tr>
					<tr>
					<td>Path to ascent.pid</td><td><input type="text" name="ascentpidpath" /></td>
					</tr>
					<tr>
					<td>Path to logon.pid</td><td><input type="text" name="logonpidpath" /></td>
					</tr>
					<tr>
					<td>Webmaster's Name:</td><td><input type="text" name="webmaster_name" /></td>
					</tr>
					<tr>
					<td>Webmaster's Email:</td><td><input type="text" name="webmaster_email" /></td>
					</tr>
					<tr>
					<td><input type="reset" value="Reset" /></td><td><input type="submit" value="Proceed" />
					</tr>
				</table>
			</form>
			</div>
		<?php
		break;
	case "1":
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> Database Software</div>	
			<div class="body">Please select the software you are using for your database.<br />
			<br />
			<br />
			<br />
			<form method="post">
			<input type="hidden" name="step" value="2" />
			<table width="500" border="0" cellspacing="5px">
			<tr><td>Database Software:</td><td><select name="software" size="1"><option value="mysql">MySQL</option><option value="postgresql">PostgreSQL</option></select></td></tr>
			<tr><td><input type="reset" value="Reset" /></td><td><input type="submit" value="Proceed" /></td></tr>
			</table>
			</form>
			</div>
		<?php
		break;
	case "error":
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> Error</div>	
			<div class="body">The installation could not progress due to the following reasons:<br />
			<br />
			<ul />
			<?php echo $_SESSION['install']['error']; ?>
			</ul>
			</div>
		<?php
		unset($_SESSION['install']);
		break;
	default:
		?>
			<div class="title"><img src="../images/websam.gif" height="20" width="20" /> WebSAM Installer</div>	
			<div class="body">This will guide you through the installation, you will just need to fill out a few small things and you will be all set!<br />
			<br />
			<br />
			<br />
			<table width="500" border="0" cellspacing="5px">
			<tr>
				<td>Begin Installation:</td>
				<td><form method="post"><input type="hidden" name="step" value="1" /><input type="submit" value="Begin" /></form> </td></tr>
			</table>
			</div>
		<?php
		break;
	}
	
	?>
		</body>
		</html>
